package com.oracle.team.service;

import static com.oracle.team.service.Data.ARCHITECT;
import static com.oracle.team.service.Data.DESIGNER;
import static com.oracle.team.service.Data.EMPLOYEE;
import static com.oracle.team.service.Data.NOTEBOOK;
import static com.oracle.team.service.Data.PC;
import static com.oracle.team.service.Data.PRINTER;
import static com.oracle.team.service.Data.PROGRAMMER;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.oracle.team.domain.Architect;
import com.oracle.team.domain.Designer;
import com.oracle.team.domain.Employee;
import com.oracle.team.domain.Equipment;
import com.oracle.team.domain.NoteBook;
import com.oracle.team.domain.PC;
import com.oracle.team.domain.Printer;
import com.oracle.team.domain.Programmer;

public class NameListServiceColl {
	
	 //private Employee[] employees;
	 private ArrayList<Employee> employees;
	
	 public NameListServiceColl() {
		 //读取excel文件，分析人员类型、创建对象实例，挂接装备，放入employees数组
		 FileInputStream fis = null;
	     Workbook workbook = null;
			
		
		try {
			fis = new FileInputStream(new File("data.xlsx"));
			workbook = new XSSFWorkbook(fis);
			Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
			
			//以下代码优化
			Sheet sheet2 = workbook.getSheetAt(1); // 获取第二个工作表
			
			int rows = sheet.getLastRowNum();	//最后一行的行索引
			//employees = new Employee[rows];	
			employees = new ArrayList<>();	
			
			//逐行读取数据
			for(int i=0; i<rows; i++) {
				//读取当前行
				Row row = sheet.getRow(i+1);
				
				//以下代码优化
				Row row2 = sheet2.getRow(i+1);
				
				//拆分单元格
				int type = Integer.parseInt(row.getCell(0).getStringCellValue());
				int id = Integer.parseInt(row.getCell(1).getStringCellValue());
				String name = row.getCell(2).getStringCellValue();
				int age = Integer.parseInt(row.getCell(3).getStringCellValue());
				double salary = Double.parseDouble(row.getCell(4).getStringCellValue());
				
				double bouns=0;
	    		int stock=0;
	    		Equipment eqipment=null;
	    		
	    		if(type == EMPLOYEE) {	//普通员工
	    			//创建员工对象
	    			//employees[i] = new Employee(id, name, age, salary);  
	    			employees.add(new Employee(id, name, age, salary));  
	    		}
	    		else if(type == PROGRAMMER)	{	//程序员    			
	    			
	    			//eqipment = getEquipment(i);
	    			eqipment = getEquipment(row2);	//传递第二个工作表对应的行数据
	    			
	    			//employees[i] = new Programmer(id, name, age, salary, 0, Status.FREE, eqipment);    	
	    			employees.add(new Programmer(id, name, age, salary, 0, Status.FREE, eqipment));
	    		}
	    		else if(type == DESIGNER) {
	    			//获取对应的装备
	    			//eqipment = getEquipment(i);
	    			eqipment = getEquipment(row2);	//传递第二个工作表对应的行数据
	    			
	    			bouns = Double.parseDouble(row.getCell(5).getStringCellValue());
	    			
	    			//employees[i] = new Designer(id, name, age, salary, 0, Status.FREE, eqipment, bouns);  
	    			employees.add(new Designer(id, name, age, salary, 0, Status.FREE, eqipment, bouns)); 
	    		}
	    		else if(type == ARCHITECT) {
	    			//eqipment = getEquipment(i);
	    			eqipment = getEquipment(row2);	//传递第二个工作表对应的行数据
	    			
	    			bouns = Double.parseDouble(row.getCell(5).getStringCellValue());
	    			stock = Integer.parseInt(row.getCell(6).getStringCellValue());
	    			
	    			//employees[i] = new Architect(id, name, age, salary, 0, Status.FREE, eqipment, bouns, stock);    			
	    			employees.add(new Architect(id, name, age, salary, 0, Status.FREE, eqipment, bouns, stock));
	    		}
	    		
			}
			
		} 
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if(workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}			
		}	
	 }
	 
	 private Equipment getEquipment(int i) {
       //为程序员、设计师、架构师创建对应设备
   	//设备二维数组找到对应元素(一维数组)
   	//获取对应的装备
   	Equipment eqipment=null;
		//int eqtype = Integer.parseInt(EQIPMENTS[i][0]);
		
		FileInputStream fis = null;
	    Workbook workbook = null;
			
		
		try {
			fis = new FileInputStream(new File("data.xlsx"));
			workbook = new XSSFWorkbook(fis);
			Sheet sheet = workbook.getSheetAt(1); // 获取第二个工作表
			
			Row row = sheet.getRow(i);
			int eqtype = Integer.parseInt(row.getCell(0).getStringCellValue());
			
			if(eqtype == PC) {	//PC
				eqipment = new PC(row.getCell(1).getStringCellValue(), row.getCell(2).getStringCellValue());    				
			}
			else if(eqtype == NOTEBOOK) {
				eqipment = new NoteBook(row.getCell(1).getStringCellValue(), Double.parseDouble(row.getCell(2).getStringCellValue()));    				
			}
			else if(eqtype == PRINTER) {
				eqipment = new Printer(row.getCell(1).getStringCellValue(), row.getCell(2).getStringCellValue());			
			}
		} 
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if(workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}			
		}
		
		return eqipment;		
	        
	}
	 
	private Equipment getEquipment(Row row) {
		Equipment eqipment=null;
		
		int eqtype = Integer.parseInt(row.getCell(0).getStringCellValue());
		if(eqtype == PC) {	//PC
			eqipment = new PC(row.getCell(1).getStringCellValue(), row.getCell(2).getStringCellValue());    				
		}
		else if(eqtype == NOTEBOOK) {
			eqipment = new NoteBook(row.getCell(1).getStringCellValue(), Double.parseDouble(row.getCell(2).getStringCellValue()));    				
		}
		else if(eqtype == PRINTER) {
			eqipment = new Printer(row.getCell(1).getStringCellValue(), row.getCell(2).getStringCellValue());			
		}
		
		return eqipment;
	}
	
	//public Employee[] getAllEmployees() {
	public ArrayList<Employee> getAllEmployees() {
       //返回所有员工的数组
		return employees;
   }

   public Employee getEmployee(int id) throws TeamException {
       //返回指定员工-循环便利，若当前员工号==id，表示查到了，返回此员工，退出循环
   	//若循环完毕仍然没有找到，则抛出异常
   	/*Employee emp = null;
   	
   	for(int i=0; i<employees.length; i++) {
   		Employee nowemp = employees[i];
   		if(nowemp.getId()==id) {
   			emp = nowemp;
   			break;
   		}    		
   	}
   	
   	//
   	if(emp!=null) {
   		return emp;
   	}
   	else {
   		throw new TeamException(id+"查无此人");
   	}*/
   	
   	//2优化代码
   	/*for(int i=0; i<employees.length; i++) {
   		Employee nowemp = employees[i];
   		if(nowemp.getId()==id) {
   			return nowemp;
   		}    		
   	}
   	throw new TeamException(id+"查无此人");*/
   	
   	//3继续优化
   	for(Employee emp : employees) {
   		if(emp.getId()==id) {
   			return emp;
   		}    		
   	}
     	throw new TeamException(id+"查无此人");    
     	
     	//4继续优化-判断id的范围，合理区间循环，否则直接抛出异常
     	
     	
   }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NameListServiceColl nameListService = new NameListServiceColl();
		System.out.println(nameListService.getAllEmployees().size());
	}

}
